const dyApi = require("../../common/douyinUtils");
const events = require("../../common/events");
const type = {
  下一个: async () => {
    log("上滑 start");
    // 上滑 next
    await dyApi.nextVideo();
    log("上滑 over");
  },
  上一个: async () => {
    log("下滑 start");
    // 下滑
    await dyApi.lastVideo();
    log("下滑 over");
  },
  点赞: async () => {
    log("点赞start");
    await dyApi.Dz();
    log("点赞over");
  },
  关注: async () => {
    log("关注start");
    await dyApi.concern();
    log("关注end");
  },
  分享: async () => {
    await dyApi.nextVideo();
  },
  评论: async () => {
    log("打开评论start");
    dyApi.toComment();
    sleep(1000);
    log("评论已打开over");
    log("开始评论start");
    dyApi.sentMsg("真棒！");
    sleep(1000);
    log("评论完成over");
    log("返回评论start");
    await dyApi.backToPl();
    log("返回评论end");
  },
  搜索: (txt="跳舞")=>{
    dyApi.backToHome()
    sleep(1000);
    dyApi.inputToSearch(txt)
    sleep(1000);
    dyApi.nextVideo()
  }
};

async function init(opt) {
  auto.waitFor();
  console.log(opt);
  // 判断和等待开启无障碍
  console.show();
  let { app, steps } = opt;
  if (app == "抖音") {
    log("打开抖音start");
    dyApi.openDy();

    log("打开抖音over");
  } else {
    launchApp(app);
  }
  sleep(1000);
  // 打开抖音

  log("单独进程检查异常start");
  // 检查异常
  dyApi.checkOthers();
  steps.forEach((item) => {
    if (type[item.val]) {
      type[item.val]();
      sleep(3000);
    }
  });
  log("执行任务over");
  if (opt.num == 1) {
    events.emit("result", opt.num);
    console.hide();
  }
  opt.num--;
  while (opt.num) {
    await init(opt);
  }
}

module.exports = init;
